package com.zhangyj.impl.actions;

import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;

import com.zhangyj.BoastException;
import com.zhangyj.action.AbstractAction;
import com.zhangyj.action.RequestAction;
import com.zhangyj.action.RequestMethod;
import com.zhangyj.db.DBUtil;
import com.zhangyj.impl.plugin.IDataService;
import com.zhangyj.impl.plugin.JSONBaseDataService;
@RequestAction(name="data")
public class DataAction extends AbstractAction {
	private static final long serialVersionUID = -728457555057889202L;

	
	@RequestMethod(name = "load")
	public void list() throws Exception {
		response.setContentType("text/xml;charset=UTF-8");
		String sqlName = request.getParameter("sql");
		Connection conn = null;
		try {
			conn = DBUtil.getConnection();
			IDataService service = new JSONBaseDataService(sqlName, conn);
			Enumeration<?> enumd = request.getParameterNames();
			while (enumd.hasMoreElements()) {
				String key = (String) enumd.nextElement();
				if (!key.equals("sql"))
					service.addPararam(key, request.getParameter(key));
			}
			service.open();
			Writer writer = response.getWriter();
			service.writerTo(writer);
			service.close();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BoastException(e);
		} catch (BoastException e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
	}

}
